﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows;
using System.ComponentModel.Composition;

namespace DynamicUIFramework
{
    [Export]
    [PartCreationPolicy(CreationPolicy.Shared)]
    class PartController
    {
        Dictionary<string, string> partMapping = new Dictionary<string, string>();
        public PartController()
        {
            if (Directory.Exists(UISetting.PartPath))
            {
                string[] fs = Directory.GetFiles(UISetting.PartPath, "*.xaml", SearchOption.AllDirectories);
                foreach (var item in fs)
                {
                    string key = Path.GetFileNameWithoutExtension(item);
                    if (partMapping.ContainsKey(key))
                        partMapping[key] = item;
                    else
                        partMapping.Add(key, item);
                }
            }
        }


        public List<string> GetParts()
        {
            return partMapping.Keys.ToList();
        }


        public FrameworkElement GetPartInstance(string key)
        {
            if (this.partMapping.ContainsKey(key))
            {
                string file = this.partMapping[key];
                return XamlHelper.LoadXaml(file);
            }
            return null;
        }
    }
}
